﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Database;

namespace ConnectDatabase
{
    public class FeeDB
    {
        DatabaseDataContext dbContext = new DatabaseDataContext();

        #region Các hàm cơ bản
        public List<tblFee> GetAll()
        {
            try
            {
                var list = from a in dbContext.tblFees select a;
                return list.ToList<tblFee>();
            }
            catch
            {
                return new List<tblFee>();
            }
        }

        public tblFee GetByKey(int id)
        {
            try
            {
                var obj = dbContext.tblFees.SingleOrDefault(a => a.Id == id);
                return obj;
            }
            catch
            {
                return null;
            }
        }

        public int Insert(tblFee obj)
        {
            try
            {
                dbContext.tblFees.InsertOnSubmit(obj);
                dbContext.SubmitChanges();
                return obj.Id;
            }
            catch
            {
                return -1;
            }
        }

        public bool Update(tblFee obj)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblFees.SingleOrDefault(a => a.Id == obj.Id);
                if (exist != null)
                {
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                isOk = false;
            }
            return isOk;
        }
        #endregion

        public tblFee SearchFee(int itineraryType, string airlineCode, int IBEType, string ContinentCode, string PassengerTypeCode)
        {
            try
            {
                var list = (from a in dbContext.tblFees
                            where a.ItineraryType == itineraryType &&
                                  a.AirlineCode == airlineCode &&
                                  a.IBEType == IBEType &&
                                  a.ContinentCode == ContinentCode &&
                                  a.PassengerTypeCode == PassengerTypeCode
                            select a).FirstOrDefault();
                return list;
            }
            catch
            {
                return null;
            }
        }
    }
}
